//
//  FR_MyAcountViewController.m
//  FaceRecognition
//
//  Created by syun on 17/2/15.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_MyAcountViewController.h"
#import "MyAccountTableViewCell.h"
#import "RankModel.h"
#import "FR_Record_InformationViewController.h"
@interface FR_MyAcountViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    int pageNum;
    NSString * logo;
    UILabel * noDataLabel;
}
@property (nonatomic, strong) UITableView * myTable;
@property (nonatomic, strong) NSMutableArray * dataArray;
@end

@implementation FR_MyAcountViewController
- (UITableView *)myTable
{
    if (_myTable == nil) {
        
        _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGH-64) style:UITableViewStylePlain];
        _myTable.delegate = self;
        _myTable.dataSource = self;
        _myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_myTable setShowsVerticalScrollIndicator:NO];
        
        _myTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self requestData];
        } ];
        
        _myTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [self requestMoreData];
        } ];
        
    }
    
    return _myTable;
}
- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navBar.navigationItem.title = @"个人统计";
    logo = [SYAccountTool ShareAccount].account.logo;
    [self.view addSubview:self.myTable];
    
    [self requestData];
    // Do any additional setup after loading the view.
}
#pragma mark - TableViewSoureData

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 58;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 24;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    RankModel * model = [self.dataArray objectAtIndex:section];
    
    UIView * tBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 24)];
    tBack.backgroundColor = [UIColor colorWithHexString:@"f5f8fa"];

    UILabel * dataLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 5, SCREENWIDTH-15, 14)];
    dataLab.font = [UIFont systemFontOfSize:14];
    dataLab.textColor = [UIColor colorWithHexString:@"979797"];
    dataLab.textAlignment = NSTextAlignmentLeft;
    
    if (model.jointime.length>10) {
        
        dataLab.text = [model.jointime substringToIndex:10];
    }

    [tBack addSubview:dataLab];

    return tBack;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyAccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyAccountcell"];

    if (cell == nil) {
        cell = [[MyAccountTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"MyAccountcell"];

    }
    
    RankModel * model = [self.dataArray objectAtIndex:indexPath.section];
    
    cell.nameLabel.text = model.username;
    
    if (model.jointime.length>11) {
        
        cell.dateLabel.text = [model.jointime substringFromIndex:11];
    }
    
    if (logo.length > 0) {
        
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",REQUESTIMAGEURL,logo]] placeholderImage:[UIImage imageNamed:@"personalsettings_switchloading_pic_"]];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == self.dataArray.count-1) {

        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 57.5, SCREENWIDTH, 0.5)];
        line.backgroundColor = [UIColor colorWithHexString:@"dedede"];
        [cell.contentView addSubview:line];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FR_Record_InformationViewController * InformationVC = [[FR_Record_InformationViewController alloc]init];
    RankModel * model = [self.dataArray objectAtIndex:indexPath.section];
    InformationVC.infoID = model.infoid;
    InformationVC.classID = model.classid;
    InformationVC.schoolID = model.scid;
    InformationVC.recordType = 1;
    [self.navigationController pushViewController:InformationVC animated:YES];
    
}
- (void)requestData {
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    
    [paramDic setObject:@"1" forKey:@"pageno"];
    [paramDic setObject:@"20" forKey:@"pagesize"];
    [paramDic setObject:USERID forKey:@"userid"];
    [paramDic setObject:[SYAccountTool ShareAccount].account.scid forKey:@"scid"];
    
    [Utils Java_get:@"api/getUserAttendance" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"array = %@%@",array,jsonObject);
        
        [self.dataArray removeAllObjects];
        
        self.myTable.hidden = NO;
        noDataLabel.hidden = YES;
        
        if (array.count == 0) {
            
            [self.myTable.mj_header endRefreshing];
            self.myTable.hidden = YES;
            [self noDataLabel];
            [self.myTable reloadData];
            return ;
            
        }
        pageNum = 2;
        
        for (NSDictionary * dic in array) {
            
            RankModel * model = [RankModel mj_objectWithKeyValues:dic];
            [self.dataArray addObject:model];
        }
        
        [self.myTable.mj_footer endRefreshing];
        [self.myTable.mj_header endRefreshing];
        [self.myTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
    
    
    /*
    RequestParam *param = [[RequestParam alloc] init];
    param.method = @"api_face_rankingoneuser";
    param.pageno = @"1";
    param.pagesize = @"20";
    param.userid = USERID;
    
    [Utils get:nil parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"array = %@%@",array,jsonObject);
        
        [self.dataArray removeAllObjects];
        
        self.myTable.hidden = NO;
        noDataLabel.hidden = YES;
        
        if (array.count == 0) {
            
            [self.myTable.mj_header endRefreshing];
            self.myTable.hidden = YES;
            [self noDataLabel];
            [self.myTable reloadData];
            return ;
            
        }
            pageNum = 2;
        
        for (NSDictionary * dic in array) {
            
            RankModel * model = [RankModel mj_objectWithKeyValues:dic];
            [self.dataArray addObject:model];
        }
        
        [self.myTable.mj_header endRefreshing];
        [self.myTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
     */

}
- (void)requestMoreData {
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    
    [paramDic setObject:@(pageNum) forKey:@"pageno"];
    [paramDic setObject:@"20" forKey:@"pagesize"];
    [paramDic setObject:USERID forKey:@"userid"];
    [paramDic setObject:[SYAccountTool ShareAccount].account.scid forKey:@"scid"];
    
    [Utils Java_get:@"api/getUserAttendance" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"array = %@%@",array,jsonObject);
        
        if (array.count == 0) {
            
            [self.myTable.mj_footer endRefreshingWithNoMoreData];
            [self.myTable reloadData];
            return ;
            
        }else if (array.count == 20){
            
            pageNum ++;
            [self.myTable.mj_footer endRefreshing];
            
        }else
        {
            [self.myTable.mj_footer endRefreshingWithNoMoreData];
        }
        
        for (NSDictionary * dic in array) {
            
            RankModel * model = [RankModel mj_objectWithKeyValues:dic];
            [self.dataArray addObject:model];
        }
        
        
        [self.myTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
    
    
    /*
    RequestParam *param = [[RequestParam alloc] init];
    param.method = @"api_face_rankingoneuser";
    param.pageno = [NSString stringWithFormat:@"%d",pageNum];
    param.pagesize = @"20";
    param.userid = USERID;
    
    
    [Utils get:nil parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"array = %@%@",array,jsonObject);

        if (array.count == 0) {
            
            [self.myTable.mj_footer endRefreshingWithNoMoreData];
            [self.myTable reloadData];
            return ;
            
        }else if (array.count == 20){
            
            pageNum ++;
            [self.myTable.mj_footer endRefreshing];
            
        }else
        {
            [self.myTable.mj_footer endRefreshingWithNoMoreData];
        }
        
        for (NSDictionary * dic in array) {
            
            RankModel * model = [RankModel mj_objectWithKeyValues:dic];
            [self.dataArray addObject:model];
        }
        
        
        [self.myTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
    */
}

- (NSString * )getCurrentData
{
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

- (void)noDataLabel
{
    noDataLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREENHEIGH/2, SCREENWIDTH, 15)];
    noDataLabel.text = @"暂无统计";
    noDataLabel.font = [UIFont systemFontOfSize:15];
    noDataLabel.textColor = RGBCOLOR(151, 151, 151);
    noDataLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:noDataLabel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
